<?php
namespace Admin\Controller;
use Think\Controller;
class CourseController extends BaseController {
    /**
     * 列表
     */
    public function index()
    {
        $title = '课程管理';

        parent::index($title);
    }

    /*
     * 获取列表数据
     */
    public function GetData()
    {
        $courses = D('Course');
        $data = $courses->pageList(0);

        parent::GetData($data);
    }

    /**
     * 新增
     */
    public function Add()
    {
        $id = I('get.id', 0);
        if (!IS_POST) {
            if ($id == 0) {
                $title = '新增课程信息';
            } else {
                $title = '修改课程信息';
                $data = D('Course')->find($id);
                $this->assign('data', $data);
            }
            $this->assign('title', $title);
            $this->assign('id', $id);
            layout('tablelayout');
            $this->display();
        } else {
            //保存页面
            header("Content-type: text/html;charset=utf-8");
            $data = D('Course');

            if ($data->create()) {
                if ($data->ID == 0) {
                    $result = $data->add();
                }else {
                    $result = $data->save();
                }

                if ($result) {
                    $this->success('操作成功！', U('index','',''), 3);
                } else {
                    $this->error('写入错误！');
                }
            } else {
                $this->error($data->getError());
            }
        }
    }

    /**
     * 删除
     */
    public function Delete()
    {
        $id = I('get.id', 0);
        header("Content-type: text/html;charset=utf-8");
        $data = D('Course');

        $result = $data->where('ID='.$id)->delete();
        if ($result) {
            $this->success('操作成功！', U('index','',''), 3);
        } else {
            $this->error('写入错误！');
        }
    }
}